package cn.rcth.controller;

import cn.rcth.base.VO.JsonResultVO;
import cn.rcth.base.domain.UserAddress;
import cn.rcth.base.msg.ResultMsg;
import cn.rcth.base.serivce.IUserAddressSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 用户地址相关
 */
@Controller
public class UserAddressController {

    @Autowired
    private IUserAddressSerivce userAddressSerivce;

    /**
     * 查询当前用户所有的地址
     * @param userId
     * @return
     */
    @RequestMapping(value = "/userAddressAll",method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg userAddressAll(Long userId){
        List<UserAddress> userAddresses =  userAddressSerivce.UserAddressAll(userId);
        return ResultMsg.successResult(userAddresses);
    }

    /**
     * 保存用户地址
     * @param userAddress  地址信息
     * @return
     */
    @RequestMapping(value = "/addUserAddressSave",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg addUserAddressSave( UserAddress userAddress ){

        if (userAddress != null){
            Map map = userAddressSerivce.addUserAddress(userAddress);
            return ResultMsg.successResult(map);
        }
        return ResultMsg.failedResult(ResultMsg.MSG_SUCCESS_DFT);
    }

    /**
     * 根据id删除地址
     * @param addUserAddressId
     * @return
     */
    @RequestMapping(value = "/deleteAddUserAddress",method = RequestMethod.POST)
    @ResponseBody
    public JsonResultVO deleteAddUserAddress(Long addUserAddressId){
        userAddressSerivce.deleteAddUserAddress(addUserAddressId);
        return JsonResultVO.success();
    }

    /**
     * 根据id查询地址
     * @param addUserAddressId
     * @return
     */
    @RequestMapping(value = "/selectAddUserAddress",method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg selectAddUserAddress(Long addUserAddressId){
         UserAddress userAddress = userAddressSerivce.selectAddUserAddress(addUserAddressId);
        return ResultMsg.successResult(userAddress);
    }
}
